// pages/details/details.js
import baseuser from '~/utils/users.js'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        statusBarHeight: 0,
        height: 46,
        jnright: 0,
        pixelRatio: 1,
        // 以上为头部数据判断

        optionid: 16,
        detailsmsg: {},
        bottommsg: [],
        detailsswiper: [],
        swiperitem: 1,
        imgheights: '300',

        // 开始领样数据
        lingyang: false,
        //保障
        show:false
    },
    guarantee(){
      // console.log(222);
this.setData({
  show:true
})
    },
    // 跳转店铺
    gotoShop(){
      wx.navigateTo({
        url: `../../pages/shophomepage/shophomepage?id=${this.data.detailsmsg.supplierId}`,
    })
    },
    // 跳转名片
    gotoBusinessCard(){
      wx.navigateTo({
        url: `/my/business_card_settings/business_card_settings?id=${this.data.detailsmsg.supplierId}`,
    })
    },
    previewimg(){
        wx.previewImage({
            urls: this.data.detailsswiper
        })
    },
    navenext() {
        wx.navigateTo({
            url: `../../pages/shophomepage/shophomepage?id=${this.data.detailsmsg.supplierId}`,
        })
    },
    copydata(items) {
        wx.setClipboardData({
            data: items.currentTarget.dataset.copys,
            success(res) {
                wx.showToast({
                    title: '拷贝成功',
                    icon: 'success',
                    duration: 2000
                })
            }
        })
    },
    nextdetail(e) {
        wx.navigateTo({
            url: `../details/details?id=${e.currentTarget.dataset.id}`,
        })
    },
    async collcompany(items){
        console.log(items)
        let setdata = {
            "proId": this.data.optionid,
        }
    
        let reqdata = await baseuser.collectrecord(setdata)
        console.log(reqdata.data);
        if (reqdata.code == 200) {
            wx.showToast({
                title: reqdata.data.data.message,
                icon: 'success',
                duration: 2000
            })
            this.setData({
                'detailsmsg.isFollow':reqdata.data.data.state
            })
        } else {
            wx.showToast({
                title: '操作失败',
                icon: 'error',
                duration: 2000
            })
        }
    },
    naveback() {
        setTimeout(() => {
            wx.navigateBack()
        }, 300);
    },
    swiperchange(e) {
        this.setData({
            swiperitem: e.detail.current
        })
    },
    imgLoads(e) {
        var imgwidth = e.detail.width,
            imgheight = e.detail.height,
            //宽高比  
            ratio = imgwidth / imgheight;
        // console.log(imgwidth, imgheight)
        //计算的高度值  
        var viewHeight = 750 / ratio;
        var imgheight = viewHeight;
        // var imgheights = this.data.imgheights;
        if (e.currentTarget.dataset.indes == 0) {
            //每一张图片对应的高度存到数组里
            // imgheights.push(imgheight);
            this.setData({
                imgheights: imgheight,
            })
        }

    },
    naveback() {
        wx.navigateBack()
    },
    alertpople() {
        this.setData({
            lingyang: true
        })
    },
    onClone() {
        this.setData({
            lingyang: false
        })
    },
    onClose2() {
      this.setData({
          show: false
      })
  },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let statusBarHeight = (0, wx.getSystemInfoSync)().statusBarHeight;
        let pixelRatio = (0, wx.getSystemInfoSync)().pixelRatio;
        let offsetwisth = (0, wx.getSystemInfoSync)().screenWidth;
        let cc = (0, wx.getSystemInfoSync)().windowWidth;
        let jntop = (0, wx.getMenuButtonBoundingClientRect)().top
        let jnwidth = (0, wx.getMenuButtonBoundingClientRect)().width;
        let jnright = (0, wx.getMenuButtonBoundingClientRect)().right;
        this.setData({
            statusBarHeight: statusBarHeight,
            height: 46 + statusBarHeight,
            jnwidth: jnwidth,
            jnright: jnright,
            offsetwisth: offsetwisth,
            pixelRatio: pixelRatio,
            // jntop:Math.abs((jntop - 47)/2),
            jntop: jntop - 47 > 0 ? Math.abs(jntop - 47) : Math.abs((jntop - 47) / pixelRatio)
        });
        console.log(options)
        if (JSON.stringify(options) != undefined && JSON.stringify(options).length > 2) {
            this.setData({
                optionid: options.id
            })
            this.getmainmsg()
        } else {
            wx.showToast({
                title: '查询失败',
                icon: 'none',
                duration: 2000
            })
            setTimeout(() => {
                this.naveback()
            }, 2000);
        }

    },
    async getmainmsg() {
        let reqdata = {
            id: this.data.optionid
        }
        let datamsg = await baseuser.particulars(reqdata)
        if (datamsg.code == 200) {
            this.setData({
                detailsmsg: datamsg.data.Result.product,
                bottommsg: datamsg.data.Result.recommend,
                detailsswiper: JSON.parse(datamsg.data.Result.product.slideshow),
            })
            for (const i in this.data.detailsmsg.cpgg) {
                var setstring = `detailsmsg.cpgg[${i}].params`
                var newdata = JSON.parse(this.data.detailsmsg.cpgg[i].params)
                this.setData({
                    [setstring]: newdata
                })
            }

        }
    },
    selectchange(data) {
        console.log(data)
        // this.setData({
        //     selectone: data.currentTarget.dataset.selectone,
        //     selecttwo: data.currentTarget.dataset.selecttwo,
        // })
        let setstring = `detailsmsg.cpgg[${data.currentTarget.dataset.selectone}].select`
        this.setData({
            [setstring]: data.currentTarget.dataset.selecttwo,
        })
    },
    async submlingyang() {
        let reqcpgg = ''
        for (const only in this.data.detailsmsg.cpgg) {
            if (this.data.detailsmsg.cpgg[only].select != null && this.data.detailsmsg.cpgg[only].select != undefined) {
                reqcpgg = reqcpgg + this.data.detailsmsg.cpgg[only].name + ":" + (this.data.detailsmsg.cpgg[only].params)[this.data.detailsmsg.cpgg[only].select].name + ","
            } else {
                wx.showToast({
                    title: `${this.data.detailsmsg.cpgg[only].name}还未选择`,
                    icon: 'none',
                    duration: 2000
                })
                return false
            }
        }
        reqcpgg = reqcpgg.substring(0, reqcpgg.length - 1)
        console.log(reqcpgg)
        var navebackdata = {
            reqcpgg,
            id: this.data.optionid
        }
        this.onClone()
        wx.navigateTo({
            url: `/my/receive/receive?id=` + this.data.optionid + "&reqcpgg=" + reqcpgg,
        })
    },
    previews() {
        let previewsurls = JSON.parse(this.data.detailsmsg.material)
        wx.previewImage({
            urls: previewsurls
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})